package com.example.hhhh;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.Toast;


import com.alibaba.android.arouter.facade.annotation.Route;

import java.util.Timer;
import java.util.TimerTask;

/**
 * @ProjectName: Hhhh
 * @Package: com.example.hhhh
 * @ClassName: FristAct
 * @Description: java类作用描述
 * @Author: songhua
 * @CreateDate: 2019/12/20 0020 16:02
 */
@Route(path = "/app/FristAct")
public class FristAct extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemSelectedListener  {

    private ProgressButton button_progress_green;
    private ButtonProgressBar mLoader;
    private Spinner mLoaderTypeSp;
    private int progress = 0;

    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_one);
        button_progress_green = findViewById(R.id.button_progress_green);
        button_progress_green.setMinProgress(0);
        button_progress_green.setMaxProgress(100);
        button_progress_green.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                test();
                //countDownTimer.start();
                //         result
                       //     cancelTimer();
            }
        });

        mLoader = (ButtonProgressBar) findViewById(R.id.cl_main);
        mLoaderTypeSp = (Spinner) findViewById(R.id.sp_select_type);
        init();

    }

    private void init() {
        mLoaderTypeSp.setOnItemSelectedListener(this);
        mLoader.setOnClickListener(this);
    }

    private void callHandler() {
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (progress <= 100) {
                    updateUI();
                    progress++;
                    callHandler();
                } else {
                    progress = 0;
                    Toast.makeText(FristAct.this, "Complete", Toast.LENGTH_SHORT).show();
                }
            }
        }, 20);
    }

    public void updateUI() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mLoader.setProgress(progress);
            }
        });
    }

    @Override
    public void onClick(View v) {
        if (mLoader.getLoaderType() == ButtonProgressBar.Type.DETERMINATE) {
            callHandler();
        } else {
            mLoader.startLoader();
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    mLoader.stopLoader();
                    Toast.makeText(FristAct.this, "Complete", Toast.LENGTH_SHORT).show();
                }
            }, 5000);
        }
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        mLoader.reset();
        switch (position) {
            case 0:
                mLoader.setLoaderType(ButtonProgressBar.Type.DETERMINATE);
                break;
            case 1:
                mLoader.setLoaderType(ButtonProgressBar.Type.INDETERMINATE);
                break;
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) { }



    /**
     * CountDownTimer 实现倒计时
     */
    private CountDownTimer countDownTimer = new CountDownTimer(3000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            String value = String.valueOf((int) (millisUntilFinished / 1000));
            button_progress_green.setProgress((int) (millisUntilFinished / 1000) + 20);
            button_progress_green.setText(value);
//            mTvValue.setText(value);
        }

        @Override
        public void onFinish() {
//            mTvValue.setText(getResources().getString(R.string.done));
        }
    };

    void test() {
        mTask = new MyTask();
        mTimer = new Timer();
        mTimer.schedule(mTask, 0, 10);
    }

    protected TimerTask mTask;
    protected Timer mTimer;

    private class MyTask extends TimerTask {
        int num = 0;

        @Override
        public void run() {
            num++;
            if (num > 80) cancelTimer();
            button_progress_green.setProgress(num);
        }
    }

    void cancelTimer() {
        if (mTask != null) mTask.cancel();
        if (mTimer != null) mTimer.cancel();
        button_progress_green.reset();
    }

}
